/*
 * StockItems.java
 *
 * Created on 20, Feb 2006: as StocksMenu
 * Renamed on July 29, 2007, 11:36 PM
 * 2008: Some modifications on this year, I did something on 2008 :)
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.menu;

import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import portfolio.mainScreen;
import portfolio.stock.ConfigureSVMPanel;
import portfolio.stock.SelectVariables;

/**
 *
 * @author  ACA
 */
    public class StockItems  implements ActionListener
    {
        private JMenu       itemsMenu          = null;        
	private Vector      stockMenu;
	private mainScreen  mainFrame          = null;
        private String      sInputDB           = null;
        private String []   sExcludeTables     = null;
        private JMenuItem   tmpMenuItem        = null;
        
        //ACA 2008
        private SelectVariables variables      = null;
        private JPanel jSelectVariablesTab     = null;

	/**
         *
         */
        public void actionPerformed (ActionEvent evt) {
            try {                                
                String sStockSelected = evt.getActionCommand ();

                if (stockMenu == null) {
                    stockMenu = mainFrame.getDBLoggedHost().getDB(sInputDB).showTables();
                    int iSize = sExcludeTables.length;
                    for (int i = 0; i < iSize; i++) {
                        stockMenu.remove(sExcludeTables [i]);
                    }
                }

                if (variables == null) {
                    variables = new SelectVariables (mainFrame, sStockSelected, null);
                    new ConfigureSVMPanel (mainFrame, sStockSelected, variables, null);
                    /////////////////////////////////////////////////////////////////////////////////////////////
                    mainFrame.displayMessage("StockItems.actionPerformed 65", "Stock selected: " + sStockSelected);
                    jSelectVariablesTab.repaint();
                } else {
                    mainFrame.repaintStockTabedPanels ();
                    
                    variables = new SelectVariables (mainFrame, sStockSelected, null);
                    new ConfigureSVMPanel (mainFrame, sStockSelected, variables, null);
                    ///////////////////////////////////////////////////////////////////////////////////////////////
                    mainFrame.displayMessage("StockItems.actionPerformed 73", "Stock selected: " + sStockSelected);
                }
            }
            catch (Exception exp) {
                mainFrame.displayMessage("StockItems.actionPerformed 79", exp.toString());
            }
        } /* End Method portfolioMainScr::stocksMenu::ActAdapter::actionPerformed */
	/**
         *
         */
	public StockItems (mainScreen Frame, JMenu menuParam)
	{
	    mainFrame  = Frame;
            itemsMenu  = menuParam;

            tmpMenuItem = new JMenuItem ("<empty list>");
	    tmpMenuItem.setHorizontalTextPosition (JButton.RIGHT);
	    tmpMenuItem.setBackground (Color.lightGray);
	    tmpMenuItem.setForeground (Color.gray);
	    itemsMenu.add (tmpMenuItem);
            
            jSelectVariablesTab = mainFrame.getSelectVariablesPanel();

	} /* End Constructor PortfolioMainScr::stocksMenu */
	/**
         *
         */
	protected JMenuItem addMenuItem (JMenu menu, String sText)
	{
	    if (menu == null || sText == null)
		return null;
	    if (sText.equals ("-")) {
		menu.addSeparator ();
		return null;
	    }

	    JMenuItem miNew = new JMenuItem (sText);
	    miNew.setHorizontalTextPosition (JButton.RIGHT);
	    miNew.setBackground (Color.lightGray);
	    miNew.setForeground (Color.gray);
            miNew.addActionListener (this);

	    menu.add (miNew);
	    return miNew;
	} /* End Method PortfolioMainScr::stocksMenu::addMenuItem */
        /**
         *
         */
        public void addMultipleMenuItems (Connection connectionDB) throws Exception
        {
            try {
                if (connectionDB != null) {
                    stockMenu = mainFrame.getDBLoggedHost().getDB(sInputDB).showTables();
                    int iSize = sExcludeTables.length;
                    for (int i = 0; i < iSize; i++) {
                        stockMenu.remove(sExcludeTables [i]);
                    }

                    if (stockMenu != null) {
                        itemsMenu.removeAll ();
                        for (int iIdx = 0; iIdx < stockMenu.size (); iIdx++) {
                            this.addMenuItem (itemsMenu, stockMenu.get (iIdx).toString ());
                        }
                        this.setBlackItemList ();
                    }
                }
                return;
            }
            catch (Exception exp) {
                throw new Exception (exp);
            }
        } /* end method addMultipleMenuItems */
        /**
         *
         */
        public void setInputDB (String sInputParam)
        {
            sInputDB = sInputParam;
            return;
        }
        /**
         *
         */
        public void setExcludeTables (String [] sExcludeParam)
        {
            sExcludeTables = sExcludeParam;
            return;
        }
        /**
         *
         */
        private void setBlackItemList ()
        {
            if (stockMenu == null)
                return;
            
            if (stockMenu.size () <= 0)
                return;
            
            for (int iIdx = 0; iIdx < stockMenu.size (); iIdx++) {
                JMenuItem tmpItem = itemsMenu.getItem (iIdx);
                tmpItem.setForeground(Color.BLACK);
            }        
            return;
        }
        /**
         * 
         */
        public void setSelectVariables (SelectVariables variablesParam)
        {
            variables = variablesParam;
            return;
        }
        /**
         *
         */
        public void updateStockItems (String sInputDB, String [] sExcludeTables) throws Exception
        {
            this.setInputDB (sInputDB);
            this.setExcludeTables (sExcludeTables);
            this.addMultipleMenuItems (mainFrame.getDBLoggedHost().getConnection());
        }
    } /* End class portfolioMainScr::stocksMenu */
